package com.ttn.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttn.business.service.CourseUnitVocabularyService;
import com.ttn.common.core.entity.PageQuery;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.business.CourseUnitVocabulary;
import com.ttn.common.core.entity.business.bo.ModifyCourseUnitVocabularySortBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * (course_unit_vocabulary)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/course-unit-vocabulary")
@Slf4j
public class CourseUnitVocabularyController {
    /**
     * 服务对象
     */
    @Resource
    private CourseUnitVocabularyService courseUnitVocabularyService;

    /**
     * 新增词汇
     *
     * @param request
     * @param courseUnitVocabulary
     * @return
     */
    @PostMapping("/save/course-unit-vocabulary")
    public Result saveCourseUnitVocabulary(HttpServletRequest request, @RequestBody CourseUnitVocabulary courseUnitVocabulary) {
        log.info("CourseUnitVocabularyController saveGrade ,参数:{}", courseUnitVocabulary);
        LambdaQueryWrapper<CourseUnitVocabulary> courseUnitVocabularyLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitVocabularyLambdaQueryWrapper.eq(CourseUnitVocabulary::getCourseUnitId, courseUnitVocabulary.getCourseUnitId());
        courseUnitVocabulary.setSort(Math.toIntExact(courseUnitVocabularyService.count(courseUnitVocabularyLambdaQueryWrapper)) + 1);
        courseUnitVocabularyLambdaQueryWrapper.eq(CourseUnitVocabulary::getWord, courseUnitVocabulary.getWord());
        //判断等级名称是否重复。不重重则直接保存
        if (courseUnitVocabularyService.count(courseUnitVocabularyLambdaQueryWrapper) > 0) {
            return Result.fail("单元内单词重复,请确认后重试");
        }
        courseUnitVocabularyService.save(courseUnitVocabulary);
        return Result.success();
    }

    /**
     * 修改词汇
     *
     * @param request
     * @param courseUnitVocabulary
     * @return
     */

    @PutMapping("/modify/course-unit-vocabulary")
    public Result modifyCourseUnitVocabulary(HttpServletRequest request, @RequestBody CourseUnitVocabulary courseUnitVocabulary) {
        log.info("CourseUnitVocabularyController saveGrade ,参数:{}", courseUnitVocabulary);
        LambdaQueryWrapper<CourseUnitVocabulary> courseUnitVocabularyLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitVocabularyLambdaQueryWrapper.eq(CourseUnitVocabulary::getWord, courseUnitVocabulary.getWord());
        courseUnitVocabularyLambdaQueryWrapper.eq(CourseUnitVocabulary::getCourseUnitId, courseUnitVocabulary.getCourseUnitId());
        courseUnitVocabularyLambdaQueryWrapper.ne(CourseUnitVocabulary::getId, courseUnitVocabulary.getId());
        //判断等级名称是否重复。不重重则直接保存
        if (courseUnitVocabularyService.count(courseUnitVocabularyLambdaQueryWrapper) > 0) {
            return Result.fail("单元内单词重复,请确认后重试");
        }
        courseUnitVocabularyService.updateById(courseUnitVocabulary);
        return Result.success();
    }

    /**
     * 修改词汇排序
     *
     * @param request
     * @param modifyCourseUnitVocabularySortBO
     * @return
     */
    @PutMapping("/modify/course-unit-vocabulary-sort")
    public Result modifyCourseUnitVocabularySort(HttpServletRequest request, @RequestBody ModifyCourseUnitVocabularySortBO modifyCourseUnitVocabularySortBO) {
        log.info("CourseUnitVocabularyController modifyCourseUnitSort ,参数:{}", modifyCourseUnitVocabularySortBO);
        CourseUnitVocabulary courseUnitVocabulary = courseUnitVocabularyService.getById(modifyCourseUnitVocabularySortBO.getId());
        LambdaQueryWrapper<CourseUnitVocabulary> courseUnitVocabularyLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitVocabularyLambdaQueryWrapper.ne(CourseUnitVocabulary::getId, modifyCourseUnitVocabularySortBO.getId());
        courseUnitVocabularyLambdaQueryWrapper.eq(CourseUnitVocabulary::getCourseUnitId, courseUnitVocabulary.getCourseUnitId());
        courseUnitVocabularyLambdaQueryWrapper.orderByAsc(CourseUnitVocabulary::getSort);
        List<CourseUnitVocabulary> courseUnitVocabularyList = courseUnitVocabularyService.list(courseUnitVocabularyLambdaQueryWrapper);
        if (courseUnitVocabularyList.size() > 0) {
            int sortIndex = 0;
            if (modifyCourseUnitVocabularySortBO.getSortingBehavior().equals(-1)) {
                if (courseUnitVocabulary.getSort() - 2 > 0) {
                    sortIndex = courseUnitVocabulary.getSort() - 2;
                }
            } else if (modifyCourseUnitVocabularySortBO.getSortingBehavior().equals(1)) {
                sortIndex = courseUnitVocabulary.getSort();
            }
            courseUnitVocabularyList.add(sortIndex, courseUnitVocabulary);
            AtomicInteger i = new AtomicInteger(1);
            courseUnitVocabularyList.stream().forEach(courseUnitGrammarTest1 -> {
                courseUnitGrammarTest1.setSort(i.getAndIncrement());
            });
            courseUnitVocabularyService.updateBatchById(courseUnitVocabularyList);
        }
        return Result.success();
    }

    /**
     * 通过id获取词汇详情
     *
     * @param request
     * @param id
     * @return
     */
    @PostMapping("/query/course-unit-vocabulary-id/{id}")
    public Result queryCourseUnitVocabularyById(HttpServletRequest request, @PathVariable("id") Integer id) {
        log.info("CourseUnitController queryCourseUnitVocabularyById ,参数id:{}", id);
        return Result.success(courseUnitVocabularyService.getById(id));
    }

    @PostMapping("/service/query/course-unit-vocabulary-course-unit-id/{courseUnitId}")
    public Result serviceQueryCourseUnitVocabularyByCourseUnitId( @PathVariable(
            "courseUnitId") Integer courseUnitId) {
        log.info("CourseUnitController serviceQueryCourseUnitVocabularyByCourseUnitId ,参数courseUnitId:{}",
                courseUnitId);
        LambdaQueryWrapper<CourseUnitVocabulary> courseUnitVocabularyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseUnitVocabularyLambdaQueryWrapper.eq(CourseUnitVocabulary::getCourseUnitId, courseUnitId);
        return Result.success(courseUnitVocabularyService.count(courseUnitVocabularyLambdaQueryWrapper));
    }

    /**
     * 批量查询词汇分页
     *
     * @param request
     * @param pageQuery
     * @return
     */
    @PostMapping("/query/course-unit-vocabulary-by-course-unit-id-page/{courseUnitId}")
    public Result queryCourseUnitVocabularyByCourseUnitIdPage(HttpServletRequest request,
                                                              @PathVariable("courseUnitId") Integer courseUnitId,
                                                              @RequestBody PageQuery pageQuery) {
        log.info("CourseUnitVocabularyController queryCourseUnitForGradePage ,参数pageQuery:{},courseUnitId:{}",
                pageQuery, courseUnitId);
        IPage<CourseUnitVocabulary> courseUnitVocabularyIPage = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        LambdaQueryWrapper<CourseUnitVocabulary> courseUnitLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseUnitLambdaQueryWrapper.eq(CourseUnitVocabulary::getCourseUnitId, courseUnitId);
        courseUnitLambdaQueryWrapper.orderByAsc(CourseUnitVocabulary::getSort);
        return Result.success(courseUnitVocabularyService.page(courseUnitVocabularyIPage, courseUnitLambdaQueryWrapper));
    }

    /**
     * 删除单个词汇
     *
     * @param request
     * @param id
     * @return
     */
    @DeleteMapping("/delete/course-unit-vocabulary-by-id/{id}")
    public Result deleteCourseUnitVocabularyById(HttpServletRequest request, @PathVariable("id") Integer id) {
        log.info("CourseUnitVocabularyController deleteCourseUnitVocabularyById ,参数id:{}", id);
        CourseUnitVocabulary courseUnitVocabulary = courseUnitVocabularyService.getById(id);
        courseUnitVocabularyService.removeById(id);
        LambdaQueryWrapper<CourseUnitVocabulary> courseUnitGrammarTestLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitGrammarTestLambdaQueryWrapper.ne(CourseUnitVocabulary::getId, id);
        courseUnitGrammarTestLambdaQueryWrapper.eq(CourseUnitVocabulary::getCourseUnitId, courseUnitVocabulary.getCourseUnitId());
        courseUnitGrammarTestLambdaQueryWrapper.orderByAsc(CourseUnitVocabulary::getSort);
        List<CourseUnitVocabulary> courseUnitVocabularyList = courseUnitVocabularyService.list(courseUnitGrammarTestLambdaQueryWrapper);
        if (courseUnitVocabularyList.size() > 0) {
            AtomicInteger i = new AtomicInteger(1);
            courseUnitVocabularyList.stream().forEach(courseUnitVocabulary1 -> {
                courseUnitVocabulary1.setSort(i.getAndIncrement());
            });
            courseUnitVocabularyService.updateBatchById(courseUnitVocabularyList);
        }

        return Result.success();
    }

}
